package monitoreomedios

import org.springframework.dao.DataIntegrityViolationException

class SpotMonitoreadoController {

    static allowedMethods = [save: "POST", update: "POST"]

    def index() {		
		if(session.usuario == null)
		{
			flash.message = "Para ingresar usted debe iniciar sesion."
			redirect(controller:"usuario", action:"login")
		}
		else
        	redirect(action: "list", params: params)
    }

    def list() {
		if(session.usuario == null)
		{
			flash.message = "Para ingresar usted debe iniciar sesion."
			redirect(controller:"usuario", action:"login")
		}
		flash.tv = []
		flash.radio = []
		def a = 0
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		def cam = Campana.get(params.id)
		if(cam !=null)
		{
			session.cam = cam.id
			session.nombrecampana = cam.nombreCampana
		}
		def c = Campana.findById(session.cam)
		def lista = SpotMonitoreado.findAllByCampana(c)
		lista.each 
		{
			println it
			def radio = MencionMultimedia.findAllBySpotAndTipoDeMedio(it,"radio")
			flash.radio[a] = radio.size
			
			def tv = MencionMultimedia.findAllBySpotAndTipoDeMedio(it,"tv")
			flash.tv[a] = tv.size

			a++

		}
		
		
		
		
		[spotMonitoreadoInstanceList: lista, spotMonitoreadoInstanceTotal: lista.count]
		
    }

    def create() {
		
		if(session.usuario == null)
		{
			flash.message = "Para ingresar usted debe iniciar sesion."
			redirect(controller:"usuario", action:"login")
		}
		
        [spotMonitoreadoInstance: new SpotMonitoreado(params)]
    }

    def save() {
        
		params.fechaRegistro = new Date()
		def c = Campana.findById(session.cam)
		c.addToSpots(new SpotMonitoreado(params)).save()
		
        redirect(action: "list")
    }

    def show() {
        def spotMonitoreadoInstance = SpotMonitoreado.get(params.id)
        if (!spotMonitoreadoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'spotMonitoreado.label', default: 'SpotMonitoreado'), params.id])
            redirect(action: "list")
            return
        }

        [spotMonitoreadoInstance: spotMonitoreadoInstance]
    }

    def edit() {
        def spotMonitoreadoInstance = SpotMonitoreado.get(params.id)
        if (!spotMonitoreadoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'spotMonitoreado.label', default: 'SpotMonitoreado'), params.id])
            redirect(action: "list")
            return
        }

        [spotMonitoreadoInstance: spotMonitoreadoInstance]
    }

    def update() {
        def spotMonitoreadoInstance = SpotMonitoreado.get(params.id)
        if (!spotMonitoreadoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'spotMonitoreado.label', default: 'SpotMonitoreado'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (spotMonitoreadoInstance.version > version) {
                spotMonitoreadoInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'spotMonitoreado.label', default: 'SpotMonitoreado')] as Object[],
                          "Another user has updated this SpotMonitoreado while you were editing")
                render(view: "edit", model: [spotMonitoreadoInstance: spotMonitoreadoInstance])
                return
            }
        }

        spotMonitoreadoInstance.properties = params

        if (!spotMonitoreadoInstance.save(flush: true)) {
            render(view: "edit", model: [spotMonitoreadoInstance: spotMonitoreadoInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'spotMonitoreado.label', default: 'SpotMonitoreado'), spotMonitoreadoInstance.id])
        redirect(action: "show", id: spotMonitoreadoInstance.id)
    }

    def delete() {
        def spotMonitoreadoInstance = SpotMonitoreado.get(params.id)
        if (!spotMonitoreadoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'spotMonitoreado.label', default: 'SpotMonitoreado'), params.id])
            redirect(controller: "Campana", action: "list")
            return
        }

        try {
            spotMonitoreadoInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'spotMonitoreado.label', default: 'SpotMonitoreado'), params.id])
            redirect(controller: "Campana", action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'spotMonitoreado.label', default: 'SpotMonitoreado'), params.id])
            redirect(controller: "Campana", action: "list")
        }
    }
}
